package com.wanbo.springbootwx.wxservlet;

import com.wanbo.springbootwx.service.WxutilService;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet("/wx")
public class WxServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 首先对接收到的消息和回复的消息进行编码规范
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        //处理消息和事件推送
        Map<String,String> reqMap =  WxutilService.parseRequest(req.getInputStream());
        System.out.println(reqMap);

        // 准备回复的数据包
        // 这块需要进行优化重写
        String respxml ="<xml>\n" +
                "  <ToUserName><![CDATA["+reqMap.get("FromUserName")+"]]></ToUserName>\n" +
                "  <FromUserName><![CDATA["+reqMap.get("ToUserName")+"]]></FromUserName>\n" +
                "  <CreateTime>"+System.currentTimeMillis()/1000 +"</CreateTime>\n" +
                "  <MsgType><![CDATA[text]]></MsgType>\n" +
                "  <Content><![CDATA[why?]]></Content>\n" +
                "</xml>";
//        String respxml = WxutilService.getRespose(reqMap);

        PrintWriter out = resp.getWriter();
        out.write(respxml);
        out.flush();
        out.close();
    }

    /**
     * 接收消息打印
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("post");
//        // 接收关注的用户发送过来的消息
//        ServletInputStream is = req.getInputStream();
//        byte [] b = new  byte[1024];
////        int len;
////        StringBuilder sb = new StringBuilder();
////        while((len=is.read(b))!= -1){
////            sb.append(new String(b,0,len));
////        }
//        System.out.println(sb.toString());
//    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("get");
        //微信平台接入验证
        /**
         *
         * signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
         * timestamp	时间戳
         * nonce	随机数
         * echostr	随机字符串
         */
        String signature = (String) req.getParameter("signature");
        String timestamp = (String) req.getParameter("timestamp");
        String nonce =  (String) req.getParameter("nonce");
        String echostr =  (String) req.getParameter("echostr");
//        System.out.println(signature);
//        System.out.println(timestamp);
//        System.out.println(nonce);
//        System.out.println(echostr);

        // 能获取到了配置信息
        // 按照规则进行校验
        if (WxutilService.check(timestamp,nonce,signature)){
            System.out.println("接入成功");
            PrintWriter out = resp.getWriter();
            //原样返回echostr完成接入校验
            out.write(echostr);
            out.flush();
            out.close();
        }else {
            System.out.println("接入失败");
        }

    }
}
